﻿using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SelfService.Helpers;
using SelfService.Model.Interface;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;

namespace SelfService.Controllers.Apis
{
    [ActionDescription("门诊")]
    [ApiController]
    [Route("Api/Clinic")]
    [AuthorizeJwtWithCookie]
    public class ClinicController : BaseApiController
    {
        private readonly IClinicControllerBase _base;

        public ClinicController(IClinicControllerBase @base)
        {
            _base = @base;
        }

        /// <summary>
        /// 查询预约信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("Query")]
        [Public]
        public async Task<ActionResult> Query()
        {
            try
            {
                Request.EnableBuffering();
                Request.Body.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(Request.Body, Encoding.UTF8);
                var body = await reader.ReadToEndAsync();
                this.Logger($":{body}");
                var data = await _base.Query(body);
                if (data.Code != 200) this.Logger($"查询门诊预约列表发生错误, ${data.Message}. 详细信息:{JsonConvert.SerializeObject(data)}", ActionLogTypesEnum.Exception);
                return this.Json(data);
            }
            catch (Exception e)
            {
                this.Logger($"查询医技申请单异常:{JsonConvert.SerializeObject(e)}", ActionLogTypesEnum.Exception);
                return BadRequest();
            }
        }

        /// <summary>
        /// 签到自动扣款
        /// </summary>
        /// <returns></returns>
        [HttpPost("Sign")]
        [Public]
        public async Task<ActionResult> Sign()
        {
            try
            {
                Request.EnableBuffering();
                Request.Body.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(Request.Body, Encoding.UTF8);
                var body = await reader.ReadToEndAsync();
                this.Logger($":{body}");
                var data = await _base.Sign(body);
                if (data.Code != 200) this.Logger($"门诊签到发生错误, ${data.Message}. 详细信息:{JsonConvert.SerializeObject(data)}", ActionLogTypesEnum.Exception);
                return this.Json(data);
            }
            catch (Exception e)
            {
                this.Logger($"签到异常:{JsonConvert.SerializeObject(e)}", ActionLogTypesEnum.Exception);
                return BadRequest();
            }
        }

        /// <summary>
        /// 挂号自动扣款
        /// </summary>
        /// <returns></returns>
        [HttpPost("Registered")]
        [Public]
        public async Task<ActionResult> Registered()
        {
            try
            {
                Request.EnableBuffering();
                Request.Body.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(Request.Body, Encoding.UTF8);
                var body = await reader.ReadToEndAsync();
                this.Logger($":{body}");
                var data = await _base.Registered(body);
                if (data.Code != 200) this.Logger($"门诊现场挂号发生错误, ${data.Message}. 详细信息:{JsonConvert.SerializeObject(data)}", ActionLogTypesEnum.Exception);
                return this.Json(data);
            }
            catch (Exception e)
            {
                this.Logger($"挂号异常:{JsonConvert.SerializeObject(e)}", ActionLogTypesEnum.Exception);
                return BadRequest();
            }
        }
    }
}
